// internal/domain/migration.go
package domain

import (
	"context"
	"time"
)

// MigrationService 迁移服务接口
type MigrationService interface {
	// 实时迁移
	MigrateVM(ctx context.Context, srcConnID, vmName, destURI string, config *MigrationConfig) (*MigrationJob, error)

	// 获取迁移任务状态
	GetMigrationStatus(ctx context.Context, jobID string) (*MigrationJob, error)

	// 取消迁移
	CancelMigration(ctx context.Context, jobID string) error

	// 离线迁移（导出/导入）
	ExportVM(ctx context.Context, connID, vmName, exportPath string) error
	ImportVM(ctx context.Context, connID, importPath string) (*VMInfo, error)
}

// MigrationConfig 迁移配置
type MigrationConfig struct {
	Live          bool          `json:"live"`           // 实时迁移
	Persistent    bool          `json:"persistent"`     // 持久化定义
	Undefine      bool          `json:"undefine"`       // 迁移后删除源
	Offline       bool          `json:"offline"`        // 离线迁移
	Compressed    bool          `json:"compressed"`     // 压缩传输
	Bandwidth     uint64        `json:"bandwidth"`      // 带宽限制 (MB/s)
	Tunnelled     bool          `json:"tunnelled"`      // 通过 libvirtd 隧道
	SharedStorage bool          `json:"shared_storage"` // 共享存储
	AutoConverge  bool          `json:"auto_converge"`  // 自动收敛
	PostCopy      bool          `json:"post_copy"`      // 后复制模式
	Parallel      int           `json:"parallel"`       // 并行连接数
	Timeout       time.Duration `json:"timeout"`        // 超时时间
}

// MigrationJob 迁移任务
type MigrationJob struct {
	ID            string          `json:"id"`
	VMName        string          `json:"vm_name"`
	SourceURI     string          `json:"source_uri"`
	DestURI       string          `json:"dest_uri"`
	Status        MigrationStatus `json:"status"`
	Progress      float64         `json:"progress"`       // 0-100
	DataRemaining uint64          `json:"data_remaining"` // bytes
	DataTotal     uint64          `json:"data_total"`     // bytes
	MemRemaining  uint64          `json:"mem_remaining"`  // bytes
	MemTotal      uint64          `json:"mem_total"`      // bytes
	StartTime     time.Time       `json:"start_time"`
	EndTime       *time.Time      `json:"end_time,omitempty"`
	Error         string          `json:"error,omitempty"`
}

type MigrationStatus string

const (
	MigrationStatusPending   MigrationStatus = "pending"
	MigrationStatusRunning   MigrationStatus = "running"
	MigrationStatusCompleted MigrationStatus = "completed"
	MigrationStatusFailed    MigrationStatus = "failed"
	MigrationStatusCancelled MigrationStatus = "cancelled"
)
